package app;


import cas.client.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

@WebServlet(name = "MainPageServlet", urlPatterns = "/main-page")
public class MainPageServlet extends HttpServlet {

    // 主页
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        request.setAttribute("account", user.getAccount());
        User[] users = OnlineUserListener.ONLINE_USERS.values().toArray(new User[0]);
        Arrays.sort(users, Comparator.comparing(User::getLoginTimestamp));
        request.setAttribute("users", users);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/MainPage.jsp");
        dispatcher.forward(request, response);
    }

}
